//
//  UserDefaultsManager.m
//  iOSProjectExtension
//
//  Created by Apple on 2020/1/10.
//  Copyright © 2020 lidianchao. All rights reserved.
//

#import "UserDefaultsManager.h"

@implementation UserDefaultsManager
+ (void)saveCookieProperties:(NSMutableDictionary *)cookieProperties key:(NSString *)key
{
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"IsClearValue"])
    {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
    NSDictionary *dic = (NSDictionary *)cookieProperties;
    [UserDefaultsManager saveDicToUserDefaults:dic key:key];
}
+ (NSMutableDictionary *)getCookieProperties:(NSString *)key
{
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"IsClearValue"])
    {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
    NSDictionary *dic = [UserDefaultsManager getDicFromDefaults:key];
    NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    return mDic;
    
}
+ (void)saveCookieArray:(NSMutableArray *)cookieArray
{
    NSArray *array = (NSArray *)cookieArray;
    [UserDefaultsManager saveArrayToUserDefaults:array key:@"cookieArray"];
}
+ (NSMutableArray *)getCookieArray
{
    NSArray *arr = [UserDefaultsManager getArrayValueFromUserDefaults:@"cookieArray"];
    NSMutableArray *cookieArray = [NSMutableArray arrayWithArray:arr];
    return cookieArray;
}

+ (void)saveLocalString:(NSString *)string withKey:(NSString *)key {
    
    if (!string.length || !key.length) {
        [UserDefaultsManager saveStringToUserDefaults:@"" key:key];
        return;
    }
    [UserDefaultsManager saveStringToUserDefaults:string key:key];
}
+ (NSString *)getLocalStringWithKey:(NSString *)key {
    
    return [UserDefaultsManager getStringValueFromUserDefaults:key];
}
+ (void)saveDeviceToken:(NSString *)deviceToken
{
    [UserDefaultsManager saveStringToUserDefaults:deviceToken key:@"device_token"];
}
+ (NSString *)getDeviceToken
{
    NSString *deviceToken = [UserDefaultsManager getStringValueFromUserDefaults:@"device_token"];
    return deviceToken;
}
+ (void)saveLanguage:(NSString *)language
{
    NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
    [stand setObject:language forKey:@"appLanguage"];
    [stand synchronize];
}
+ (NSString *)getLanguage
{
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"];
    return string;
}
/***********************************************************************/
//存储string
+ (void)saveStringToUserDefaults:(NSString *)string key:(NSString *)key
{
    NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
    [stand setObject:string forKey:key];
    [stand synchronize];
}
+ (NSString *)getStringValueFromUserDefaults:(NSString *)key
{
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    return string;
}
//存储array
+ (void)saveArrayToUserDefaults:(NSArray *)array key:(NSString *)key
{
    NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
    NSString *jsonString = [UserDefaultsManager jsonStringWithObject:array];
    [stand setObject:jsonString forKey:key];
    [stand synchronize];
}
+ (NSArray *)getArrayValueFromUserDefaults:(NSString *)key
{
    NSString *jsonString = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSArray *array = [UserDefaultsManager arrayToJSON:jsonString];
    return array;
}
+ (void)saveDicToUserDefaults:(NSDictionary *)dic key:(NSString *)key
{
    NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
    NSString *jsonString = [UserDefaultsManager jsonStringWithObject:dic];
    [stand setObject:jsonString forKey:key];
    [stand synchronize];
}
+ (NSDictionary *)getDicFromDefaults:(NSString *)key
{
    NSString *jsonString = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSDictionary *dic = [UserDefaultsManager dictionaryWithJsonString:jsonString];
    return dic;
}
/************************************对象转Json穿************************************************************/
+ (NSString *)jsonStringWithObject:(id)object
{
    id saveObject = object;
    if([saveObject isKindOfClass:[NSString class]])
    {
        saveObject = @[object];
    }
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:saveObject options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}
/************************************Json转对象************************************************************/
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                         
                                                        options:NSJSONReadingMutableContainers
                         
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}
+ (NSArray *)arrayToJSON:(NSString *)jsonStr {
    if (jsonStr)
    {
        id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
        
        if (tmp)
        {
            if ([tmp isKindOfClass:[NSArray class]])
            {
                return tmp;
            }
            else if([tmp isKindOfClass:[NSString class]]
                    || [tmp isKindOfClass:[NSDictionary class]])
            {
                return [NSArray arrayWithObject:tmp];
            }
            else
            {
                return nil;
            }
        }
        else
        {
            return nil;
        }
    }
    else
    {
        return nil;
    }
}
@end
